package com.dk.service.impl;

import com.dk.mapper.TabCategoryMapper;
import com.dk.pojo.TabCategory;
import com.dk.service.CategoryService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    String key = "Route:Category";
    @Autowired
    TabCategoryMapper categoryMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public List<TabCategory> getInfo() {
        return categoryMapper.getInfo();
    }

    @Override
    public int insertInfo(TabCategory category) {

        ValueOperations opsForValue = redisTemplate.opsForValue();
        int i = categoryMapper.insertSelective(category);
        //数据库添加成功
        if (i > 0) {
            //删除redis缓存
            redisTemplate.delete(key);
        }
        return i;
    }

    @Override
    public int updateCategory(TabCategory category) {
        int i = categoryMapper.updateByPrimaryKeySelective(category);
        //数据库更新成功
        if (i > 0) {
            //删除redis缓存
            redisTemplate.delete(key);
        }
        return i;
    }

    @Override
    public TabCategory getInfoById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteCategoryById(Integer id) {
        int i = categoryMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            //删除redis缓存
            redisTemplate.delete(key);
        }
        return i;

    }

    /**
     * 上移
     * @param cid
     * @return
     */

    @Override
    public boolean updateUpCategory(Integer cid) {
        //根据cid查询 获取对应的排序字段count
        int a = 0;
        TabCategory tabCategory = categoryMapper.selectByPrimaryKey(cid);
        Integer count = tabCategory.getCount();
        a = count;
        //根据排序字段count 查询出大于count的下一条数据
        TabCategory nextCategory = categoryMapper.getNextCountByCount(count);
        tabCategory.setCount(nextCategory.getCount());
        //修改下一位的信息为原来的信息
        categoryMapper.updateByPrimaryKey(tabCategory);
        nextCategory.setCount(a);
        //修改原来的为下一位的信息
        int i = categoryMapper.updateByPrimaryKey(nextCategory);
        redisTemplate.delete(key);
        return i>0;
    }
    /**
     * 下移
     * @param cid
     * @return
     */
    @Override
    public boolean updateDownCategory(Integer cid) {
        //根据cid查询 获取对应的排序字段count
        int a = 0;
        TabCategory tabCategory = categoryMapper.selectByPrimaryKey(cid);
        Integer count = tabCategory.getCount();
        a = count;
        //根据排序字段count 查询出大于count的上一条数据Count
        TabCategory lastcategory = categoryMapper.getlastCountByCount(count);
        tabCategory.setCount(lastcategory.getCount());
        //修改下一位的信息为原来的信息
        categoryMapper.updateByPrimaryKeySelective(tabCategory);
        lastcategory.setCount(a);
        //修改原来的为下一位的信息
        int i = categoryMapper.updateByPrimaryKeySelective(lastcategory);
        redisTemplate.delete(key);
        return i>0;
    }


}
